/*
 * Created by JFormDesigner on Wed Jun 10 16:11:08 BST 2009
 */

package gui;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import resultsDatabase.Field;

//import com.jgoodies.forms.factories.*;

/**
 * @author Alexandros Ouzounis
 */
public class WindowSelectFieldsToMine extends JInternalFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6467414430801871535L;
	private ArrayList<JList> lists = new ArrayList<JList>();
	private ArrayList<Field> fieldsList = new ArrayList<Field>();
	public WindowSelectFieldsToMine() {
		
		initComponents();
		
	}

	public void buttonCancelActionPerformed() {
		this.dispose();
	}

	public void buttonMineActionPerformed(ActionListener e) {
		buttonMine.addActionListener(e);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Alexandros
		// Ouzounis
		dialogPane = new JPanel();
		panelAvailableCategories = new JPanel();
		tabbedPane = new JTabbedPane();
		labelAvailableCategories = new JLabel();
		panelSelectedFields = new JPanel();
		panelSelectedFieldsButtons = new JPanel();
		contentPanel = new JPanel();
		scrollPane = new JScrollPane();
		modelStatus = new DefaultListModel();
		list = new JList(modelStatus);
		buttonBar2 = new JPanel();
		buttonDeleteAll = new JButton();
		buttonDeleteSelected = new JButton();
		labelSelectedFields = new JLabel();
		buttonBar = new JPanel();
		buttonMine = new JButton();
		buttonCancel = new JButton();
		labelStatus = new JLabel();

		// ======== this ========
		setTitle("Select Fields to Mine");
		setClosable(true);
		setResizable(true);
		setMaximizable(true);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		// ======== dialogPane ========
		{			
			dialogPane.setLayout(new GridLayout(2,1));

			// ======== panelAvailableCategories ========
			{
				panelAvailableCategories.setLayout(new BorderLayout());
				panelAvailableCategories.add(tabbedPane, BorderLayout.CENTER);

				// ---- labelAvailableCategories ----
				labelAvailableCategories.setText("Availabe Categories:");
				panelAvailableCategories.add(labelAvailableCategories,
						BorderLayout.NORTH);
			}
			dialogPane.add(panelAvailableCategories);

			// ======== panelSelectedFields ========
			{
				panelSelectedFields.setLayout(new BorderLayout());

				// ======== contentPanel ========
				{
					contentPanel.setLayout(new GridLayout());

					// ======== scrollPane ========
					{
						scrollPane.setViewportView(list);
						scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
						scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
					}
					contentPanel.add(scrollPane);
				}
				
				// ---- labelStatus ----
				labelStatus.setText("No fields selected!");
				
				
				
				panelSelectedFields.add(contentPanel, BorderLayout.CENTER);
				panelSelectedFieldsButtons.setLayout(new BorderLayout());
				// ======== buttonBar2 ========
				{
					buttonBar2.setBorder(new EmptyBorder(12, 0, 0, 0));
					buttonBar2.setLayout(new GridBagLayout());
					((GridBagLayout) buttonBar2.getLayout()).columnWidths = new int[] {
							0, 85, 80 };
					((GridBagLayout) buttonBar2.getLayout()).columnWeights = new double[] {
							1.0, 0.0, 0.0 };

					// ---- buttonDeleteAll ----
					buttonDeleteAll.setText("Delete All");
					buttonDeleteAll.addActionListener(new DeleteAll());
					buttonBar2.add(buttonDeleteAll, new GridBagConstraints(1,
							0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 10), 0,
							0));

					// ---- buttonDeleteSelected ----
					buttonDeleteSelected.setText("Delete Selected");
					buttonDeleteSelected.addActionListener(new DeleteSelected());
					buttonBar2.add(buttonDeleteSelected,
							new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
									GridBagConstraints.CENTER,
									GridBagConstraints.BOTH, new Insets(0, 0,
											0, 5), 0, 0));
					
					
					
				}
				panelSelectedFieldsButtons.add(buttonBar2, BorderLayout.SOUTH);
				panelSelectedFieldsButtons.add(labelStatus, BorderLayout.NORTH);

				// ---- labelSelectedFields ----
				labelSelectedFields.setText("Selected Fields:");
				panelSelectedFields
						.add(panelSelectedFieldsButtons, BorderLayout.SOUTH);
				panelSelectedFields
				.add(labelSelectedFields, BorderLayout.NORTH);
			}
			dialogPane.add(panelSelectedFields);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		
		// ======== buttonBar ========
		{
			buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
			buttonBar.setLayout(new GridLayout(1, 3));
			
			
			// ---- buttonMine ----
			buttonMine.setText("Mine ");
			buttonBar.add(buttonMine);

			// ---- buttonCancel ----
			buttonCancel.setText("Cancel");
			buttonCancel.addMouseListener(new MouseListener() {
				public void mouseClicked(MouseEvent e) {
					buttonCancelActionPerformed();
					
				}

				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
			});
			buttonBar.add(buttonCancel);
		}
		contentPane.add(buttonBar, BorderLayout.SOUTH);
		// //GEN-END:initComponents
	}

	public void addTab(String title, ArrayList<String> values) {
		DefaultListModel modelStatus = new DefaultListModel();
		JList jList = new JList(modelStatus);
		for (String s : values) {
			modelStatus.addElement(s);
		}
		jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		MyListSelectionListener l = new MyListSelectionListener();
		jList.addListSelectionListener(l);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setViewportView(jList);
		lists.add(jList);
		tabbedPane.add(title, scrollPane);
	}

	public ArrayList<Field> getSelectedFields() {
		return fieldsList;
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Alexandros Ouzounis
	private JPanel dialogPane;
	private JPanel panelAvailableCategories;
	private JTabbedPane tabbedPane;
	private JLabel labelAvailableCategories;
	private JPanel panelSelectedFields;
	private JPanel panelSelectedFieldsButtons;
	private JPanel contentPanel;
	private JScrollPane scrollPane;
	private JList list;
	public DefaultListModel modelStatus;
	private JPanel buttonBar2;
	private JButton buttonDeleteAll;
	private JButton buttonDeleteSelected;
	private JLabel labelSelectedFields;
	private JPanel buttonBar;
	private JButton buttonMine;
	private JButton buttonCancel;
	private JLabel labelStatus;

	// JFormDesigner - End of variables declaration //GEN-END:variables

	class MyListSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			Object o = e.getSource();
			JList currentList;
			int index=0;
			Dimension dim = list.getSize();
			for (JList l : lists) {
				if (o.equals(l)) {
					currentList = (JList) o;
					Field f = new Field(tabbedPane.getTitleAt(index),currentList.getSelectedValue().toString());
					fieldsList.add(f);
					modelStatus.addElement("Category: "+f.getCategory()+" | Field: "+f.getField());
					break;
				}
				index++;
			}
			removeDublicate();
			updateLabelStatus();
			list.setSize(dim);
		}
		private void removeDublicate(){
			for(int i=0;i<modelStatus.getSize();i++){
				for (int k=i+1;k<modelStatus.getSize();k++){
					if(modelStatus.getElementAt(i).equals(modelStatus.getElementAt(k))){
						modelStatus.remove(k);
						fieldsList.remove(k);
					}
				}
			}
			
		}
	}
	
	private class DeleteAll implements ActionListener { 
		 public void actionPerformed(ActionEvent e) { 
             modelStatus.clear();
             fieldsList.clear();
             updateLabelStatus();
        }
	}

	private class DeleteSelected implements ActionListener { 
		 public void actionPerformed(ActionEvent e) { 
			 int[] elements = list.getSelectedIndices();
			 for (int i=0;i<elements.length;i++){
				 modelStatus.remove(i);
				 fieldsList.remove(i);
			 }
			 updateLabelStatus();
			
       }
	}
	
	private void updateLabelStatus(){
		labelStatus.setText("Selected Fields: "+modelStatus.getSize());
	}
}
